package com.cneport.das.server.util.wmq;

import com.cneport.das.common.utils.JsonUtil;
import com.cneport.das.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.jms.core.JmsOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import javax.jms.Message;
import javax.jms.TextMessage;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author GavinZhang
 * @version 1.0
 * @desc： IBM MQ使用配置
 * @company 东方物通科技有限（北京）公司
 * @date 2021/8/19 16:05
 */
@Slf4j
@Component
@ConditionalOnProperty(name = "mq.wmq.enable", havingValue = "true")
public class WebSphereMQHelper {

    private final static int DEFAULT_NUM = 100;

    @Autowired
    private QueueOperator creator;

    @Autowired
    @Qualifier("wmqOption")
    JmsOperations jmsTemplate;

    @Autowired
    private MqParam mqParam;


    public String receive(JmsOperations jmsOperations, String destination) {
        return receive(jmsOperations, destination, 1);
    }

    public String receive(String destination) {
        return receive(jmsTemplate, destination, 1);
    }

    public String receive(String destination, int cnt) {
        return receive(jmsTemplate, destination, cnt);
    }

    public String receive(JmsOperations jmsOperations, String destination, int cnt) {
        List<String> list = new ArrayList();
        if (jmsOperations != null) {
            if (cnt == 0 || cnt > 100) {
                cnt = DEFAULT_NUM;
            }
            try {
                for (int i = 0; i < cnt; i++) {
                    Message message = jmsOperations.receive(destination);
                    if (message != null) {
                        if (message instanceof TextMessage) {
                            String msg = ((TextMessage) message).getText();
                            if (msg != null) {
                                msg = Base64Utils.encodeToUrlSafeString(msg.getBytes(StandardCharsets.UTF_8));
                                list.add(msg);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "-1";
            }
        }
        return JsonUtil.toJsonString(list);
    }

    public boolean send(JmsOperations jmsOperations, String Destination, String message) {
        if (jmsOperations != null) {
            try {
                jmsOperations.convertAndSend(Destination, message);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return false;
    }

    public boolean send(String destination, String message) {
        return send(jmsTemplate, destination, message);
    }

    public boolean createQueue(String qName, String corpName) {
        if (!StringUtil.isEmpty(qName)) {
            try {
                MQConnAgent mqConnAgent = new MQConnAgent(mqParam);
                creator.createQueue(mqConnAgent, qName, corpName, false);
                mqConnAgent.DestroyAgent(mqConnAgent.getAgent());
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public Set<String> listQueueNames() {

        Set<String> set = new HashSet<>();
        try {
            MQConnAgent mqConnAgent = new MQConnAgent(mqParam);
            set = creator.listQueueNames(mqConnAgent);
            mqConnAgent.DestroyAgent(mqConnAgent.getAgent());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return set;
    }
}
